<?php

namespace app\services\activity;

use app\model\order\OrderItems;
use app\model\order\StoreOrderItem;
use Carbon\Carbon;

class MarketingService
{
    public function getList($type, $id, $limit = 15)
    {
        if (!in_array($type, [1, 2, 3]) || !$id) return ['count' => 0, 'list' => []];

        $query = StoreOrderItem::with(['mainOrder', 'user'])->hasWhere('mainOrder', function ($query) use ($type, $id) {
            if ($type == 3) {
                $query->where('coupon_id', $id);
            }
            $query->whereNotIn('status', [4])->where('is_del', 0);
        });

        switch ($type) {
            case 1: // 秒杀
                $query->where('seckill_id', $id);
                break;
            case 2: // 满赠
                $query->where('free_id', $id);
                break;
        }

        $oderItems = $query->where('StoreOrderItem.is_del', 0)->paginate($limit);

        $list = [];
        foreach ($oderItems->getCollection() as $item) {
            $list[] = [
                'id'    =>  $item->id,
                'add_time'  =>  $item['add_time'] ? Carbon::parse($item['add_time']) : '',
                'user_name' =>  $item->user ? $item->user->nickname : '',
                'phone' =>  $item->user ? $item->user->phone : '',
                'order_sn'  =>  $item->mainOrder ? $item->mainOrder->sn : '',
                'fact_price'    =>  $item->fact_price
            ];
        }

        return [
            'list'  =>  $list,
            'count' =>  $oderItems->count()
        ];
    }
}